package org.tangolyricstagger;

import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.tangolyricstagger.model.MatchResult;
import org.tangolyricstagger.model.TagInfo;

import uk.ac.shef.wit.simmetrics.similaritymetrics.Levenshtein;

public class MatchTitle {

	public MatchResult findMatch(String originalTitle, String[] titles, TagInfo tagInfo) throws Exception {
		SortedSet<MatchResult> possibleMatchResults = new TreeSet<MatchResult>();
		List<String> possibleTitles = tagInfo.getPossibleTitles();
		for (String candidateTitle : possibleTitles) {
			float[] similarities = new Levenshtein().batchCompareSet(titles, candidateTitle);
			int highestSimilarityIndex = 0;
			for (int i = 0; i < similarities.length; i++) {
				if (similarities[i] > similarities[highestSimilarityIndex]) {
					highestSimilarityIndex = i;
				}
			}
			float similarity = similarities[highestSimilarityIndex];
			String matchedTitle = titles[highestSimilarityIndex];
			if (similarity >= 0.8) {
				possibleMatchResults.add(new MatchResult(originalTitle, candidateTitle, tagInfo, matchedTitle, similarity, true));
			} else {
				possibleMatchResults.add(new MatchResult(originalTitle, candidateTitle, tagInfo, matchedTitle, similarity, false));
			}
		}
		for (MatchResult mr : possibleMatchResults) {
			System.out.println(mr.getSimilarity());
		}
		return possibleMatchResults.last(); // return the match result with the highest similarity
	}
}
